$().ready(function() {
	$(function(){
		$(".field").fieldClear();	
	})
	
	$.fn.fieldClear = function(){
		return this.each(function(){
			var input = $("textarea,input", this);
			var label = $("label", this);
			var field = this;
			
			input.live({
				focus: function(){
					label.animate({
						opacity:0.4
					},200)
				},
				blur : function(){
					label.animate({
						opacity:1
					},200)
				},
				keyup: key,
				keydown: key
			})
			function key(){
				if(input.val()=='') {
					label.removeClass("hide")
					$('.userError',field).hide('slide');
				} else {
					label.addClass("hide")
				}
			}
		})
	}
	
	
	
	
	
	
	
	// Движение стрелочек в меню слева
	$(".left-menu").hover(function() {
		$(this).find(".left-menu-picture").animate({'left' : '13px'}, 150);
	}, function() {
		$(this).find(".left-menu-picture").animate({'left' : '0px'}, 150);
	});
	
	$('#fast-order-form').submit(function() {
		order_name = $('#Order_order_name',this).val();
		description = $('#Order_description',this).val();
		city =  $('#Order_city',this).val();
		if($(this).fullValidate()) {
			$.ajax({
				dataType: 'json',
				url: W_AJAX + 'order/check_login',
				type: 'POST',
				success: function(data) {
					logInFlag = data.result;
					content = 	'<form id="fast-order-form-full">' + 
									'<table>' +
										'<tr><td>Заголовок заказа</td><td><input type="text" id="Order_order_name" name="Order[order_name]" class="mandatory" value="' + order_name + '"></td></tr>' +
										'<tr><td>Описание работ</td><td><textarea id="Order_description" name="Order[description]" class="mandatory">' + description + '</textarea></td></tr>' +
										'<tr><td>Город</td><td>' + city + '</td></tr>' +
										'<tr><td>Контактный телефон</td><td><input type="text" id="Order_phone" name="Order[phone]"></td></tr>' +
										'<tr><td>Ориентировочный бюджет</td><td><input type="text" id="Order_cost" name="Order[cost]"></td></tr>';
					if (logInFlag == 'false') {
						content +=  '<tr><td>Имя</td><td><input type="text" id="Order_first_name" name="Order[first_name]" class="mandatory"></td></tr>' +
									'<tr><td>Фамилия</td><td><input type="text" id="Order_last_name" name="Order[last_name]" class="mandatory"></td></tr>' +
									'<tr><td>Почта</td><td><input type="text" id="Order_e_mail" name="Order[e_mail]" maxlength="128" class="mandatory"></td></tr>' +
									'<tr><td>Пароль</td><td><input type="password" id="Order_pass" name="Order[pass]" class="mandatory pass"></td></tr>' +
									'<tr><td>Подтверждение</td><td><input type="password" id="Order_pass_confirmation" name="Order[pass_confirmation]" class="confirm"></td></tr>';
					}
					content +=	'</table>' +
								'<img src="/auth/kaptcha" class="kaptcha" id="reg_crypt" alt="">' +
								'<input type="text" id="Order_kaptcha" name="Order[kaptcha]" class="kaptchaInput mandatory" maxlength="6">'+
								'<input type="submit" id="Order_save" name="Order[save]" value="Отправить">' +
								'</form>';
				jQuery.fancybox({
						'content': content
					});
				}	
			});
		}
		return false;
	});
	$('#fast-order-form-full').live('submit',function() {
		if($(this).fullValidate()) {
			order_name = $('#Order_order_name',this).val();
			description = $('#Order_description',this).val();
			
			$.ajax({
				dataType: 'json',
				url: W_AJAX + 'order/fast_add',
				type: 'POST',
				data: {
						order_name: order_name,
						description: description,
						city: city,
						phone: $('#Order_phone',this).val(),
						cost: $('#Order_cost',this).val(),
						first_name: $('#Order_first_name',this).val(),
						last_name: $('#Order_last_name',this).val(),
						pass: $('#Order_pass',this).val(),
						pass_confirmation: $('#Order_pass_confirmation',this).val(),
						e_mail: $('#Order_e_mail',this).val(),
						pass_confirmation: $('#Order_pass_confirmation',this).val(),
						kaptcha: $('#Order_kaptcha',this).val()
					},
				success: function(data) { 
					if(data.result == 'OK') {
						url = W_ABSPATH + 'orders/' + data.orderId;
						location.href = url;
					}
					else if (data.error == 'mailDublicated') {
						$('#Order_e_mail').after('<div class="userError">Эта почта уже используется<div>');
					}
					else if (data.error == 'kaprchaError') {
						$('#Order_kaptcha').after('<div class="userError">Неверные данные<div>');
					}
				}	
			});
		}
		return false;
	});
	
	
		
});